package com.flyone.herbal.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.flyone.herbal.annotation.AuthCheck;
import com.flyone.herbal.common.BaseResponse;
import com.flyone.herbal.common.DeleteRequest;
import com.flyone.herbal.common.ErrorCode;
import com.flyone.herbal.common.ResultUtils;
import com.flyone.herbal.constant.UserConstant;
import com.flyone.herbal.exception.BusinessException;
import com.flyone.herbal.exception.ThrowUtils;
import com.flyone.herbal.model.dto.user.*;
import com.flyone.herbal.model.entity.User;
import com.flyone.herbal.model.vo.LoginUserVO;
import com.flyone.herbal.model.vo.TotalVO;
import com.flyone.herbal.model.vo.UserVO;
import com.flyone.herbal.service.AdminService;
import com.flyone.herbal.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 用户接口
 */
@RestController
@RequestMapping("/admin")
@Slf4j
public class AdminController {

    @Resource
    private UserService userService;

    @Resource
    private AdminService adminService;

    @PostMapping("/login")
    public BaseResponse<LoginUserVO> adminLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        String userEmail = userLoginRequest.getUserEmail();
        String password = userLoginRequest.getPassword();
        if (StringUtils.isAnyBlank(userEmail)) {
            return null;
        }
        LoginUserVO loginUserVO = userService.adminLogin(userEmail, password, request);
        return ResultUtils.success(loginUserVO);
    }

    /**
     * 用户注销
     *
     * @param request
     * @return
     */
    @PostMapping("/logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request) {
        if (request == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = userService.adminLogout(request);
        return ResultUtils.success(result);
    }

    /**
     * 获取当前登录用户
     *
     * @param request
     * @return
     */
    @GetMapping("/get/login")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request) {
        User user = userService.getAdminUser(request);
        return ResultUtils.success(userService.getLoginUserVO(user));
    }

    /**
     * 根据 id 获取用户（仅管理员）
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<User> getUserById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(user);
    }

    /**
     * 根据 id 获取包装类
     *
     * @param id
     * @param request
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<UserVO> getUserVOById(long id, HttpServletRequest request) {
        BaseResponse<User> response = getUserById(id, request);
        User user = response.getData();
        return ResultUtils.success(userService.getUserVO(user));
    }

    @GetMapping("/get/all")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<TotalVO> getAllNum(HttpServletRequest request) {
        TotalVO totalVO = adminService.getAllNum();
        return ResultUtils.success(totalVO);
    }
}
